package com.data.test;

import io.minio.MinioClient;
import io.minio.errors.MinioException;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author Str
 * @Date 2022/2/4 23:02
 * @Description :上传对象到服务器
 */
public class PutObject {
    /**
     * MinioClient.putObject() example.
     */
    public static void main(String[] args) throws Exception {
        try {
            MinioClient minioClient = new MinioClient(
                    "http://139.224.247.174:9000/",
                    "admin", "admin123");
            System.out.println("minioClient：" + minioClient);
            //  fields字段表示要抓取的数据类型。
            String urlPath = "http://quotes.money.163.com/service/chddata.html?code=0600000&start=20200101&end=20200107&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP";

            System.out.println("urlPath："  + urlPath);

            // 统一资源
            URL url = new URL(urlPath);
            // 连接类的父类，抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
            //设置超时
            httpURLConnection.setConnectTimeout(1000 * 5);
            //设置请求方式，默认是GET
//            httpURLConnection.setRequestMethod("POST");
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            // 打开到此 URL引用的资源的通信链接（如果尚未建立这样的连接）。
            httpURLConnection.connect();
            // 文件大小
//            int fileLength = httpURLConnection.getContentLength();

            // 控制台打印文件大小
//            System.out.println("您要下载的文件大小为:" + fileLength / 1024 + "KB");

            // 建立链接从请求中获取数据
//            URLConnection con = url.openConnection();
            InputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
            System.out.println("url：" + bin);
            {
                // 为对象上传创建一个 InputStream。
                ByteArrayInputStream inputStream = new ByteArrayInputStream(
                        "hello world".getBytes("UTF-8"));
                // 使用输入流中的内容在 my-bucketname 中创建对象 my-objectname
                minioClient.putObject(
                        "stock", "test2.csv", bin, bin.available(), "application/octet-stream");
                inputStream.close();
                bin.close();
                System.out.println("my-objectname is uploaded successfully");
            }

//            {
//                // 将本地文件上传到 my-bucketname 存储桶
//                minioClient.putObject("my-bucketname", "my-objectname2",
//                        "D:\\tmp\\images.zip", null);
//                System.out.println("my-objectname2 is uploaded successfully");
//            }

        } catch (MinioException e) {
            e.printStackTrace();
        }
    }
}